package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.mi.MiObject;
import com.example.myapplication.miadapter.MiAdapter;
import com.example.myapplication.mienum.MiObjectType;

import java.util.ArrayList;
import java.util.List;

public class MiHomeActivity extends AppCompatActivity {
    private List<String> roomList=new ArrayList<>();
    private List<MiObject> objectList=new ArrayList<>();
    private ActivityResultLauncher<Intent> launcher;
    private RecyclerView recyclerView;
    private MiAdapter miAdapter;


    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_mi_home);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initEvents();
        initData();
        launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK&&result.getData() != null) {
                        //拿到返回的数据
                        String deviceName = result.getData().getStringExtra("device_name");
                        MiObjectType miObjectType=result.getData().getSerializableExtra("device_type", MiObjectType.class);
                        String deviceTip = result.getData().getStringExtra("device_tip");
                        MiObject miObject = new MiObject(deviceName, miObjectType);
                        miObject.setTip(deviceTip);
                        objectList.add(miObject);
                        miAdapter.notifyDataSetChanged();
                    }
                });
        recyclerView=findViewById(R.id.recyclerViewMi);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(layoutManager);
        miAdapter = new MiAdapter(objectList);
        recyclerView.setAdapter(miAdapter);

    }
    private void initEvents(){
        Toolbar toolbar=findViewById(R.id.toolbar);
        toolbar.setOnMenuItemClickListener((menuItem)->{
            if (menuItem.getItemId()==R.id.add_object){
                Intent intent = new Intent(MiHomeActivity.this, AddProjectActivity.class);
                launcher.launch(intent);
            }
            return false;
        });
    }
    private void initData(){
        MiObject miObject = new MiObject();
        miObject.setMiObjectType(MiObjectType.FAN);
        miObject.setDeviceName("米家循环扇");
        miObject.setTip("经济实惠");
        objectList.add(miObject);
    }
}